package sk.stuba.fiit.repka.dp.db;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

public class DBAttributesManipulator implements DataManipulator{
	private final String INSERT_ATTR_QUERY = "INSERT INTO ATTRIBUTE(ID_ATTRIBUTE_TYPE, VALUE ) VALUES (?, ?)";
	
	private DatabaseController dbControl;
	private Map<String, String> attributes;
	private int expectationsID;
	
	public DBAttributesManipulator(DatabaseController dbControl, Map<String, String> attributes, int expectationsId) {
		this.dbControl = dbControl;
		this.attributes = attributes;
		this.expectationsID = expectationsId;
	}
	
	private int getTypeID(String type) throws SQLException {
		String query = "SELECT * FROM ATTRIBUTE_TYPE where type_name = ?";
		PreparedStatement statement = this.dbControl.getCon().prepareStatement(query);
		statement.setString(1, type);
		ResultSet rs = statement.executeQuery();
		int result = -1;
		while(rs.next()) {
			result = rs.getInt("ID");
		}
		
		rs.close();
		statement.close();
		return result;
	}
	
	private void mapAttributeToExpectations(int attrID) throws SQLException { 
		String query = "INSERT INTO EXPECTATIONS_ATTRIBUTES(\"ID_ATTRIBUTE\", \"ID_EXPECTATION\" ) VALUES (?, ?)";
		PreparedStatement statement = this.dbControl.getCon().prepareStatement(query);
		statement.setInt(1, attrID);
		statement.setInt(2, this.expectationsID);
		int count = statement.executeUpdate();
	}
	
	private boolean existInDatabase(String value) throws SQLException {
		boolean result = false;
		String query = "SELECT * FROM ATTRIBUTE WHERE VALUE=?";
		
		PreparedStatement statement = this.dbControl.getCon().prepareStatement(query);
		statement.setString(1, value);
		ResultSet rs = statement.executeQuery();
		if(rs.next()) {
			result = true;
		}
		
		rs.close();
		statement.close();
		
		return result;
	}
	
	private int getCurrenltyID(String value) throws SQLException {
		int result = -1;
		String query = "SELECT * FROM ATTRIBUTE WHERE VALUE=?";
		
		PreparedStatement statement = this.dbControl.getCon().prepareStatement(query);
		statement.setString(1, value);
		ResultSet rs = statement.executeQuery();
		if(rs.next()) {
			result = rs.getInt("ID");
		}
		
		rs.close();
		statement.close();
		
		return result;
	}
	
	@Override
	public boolean insert() throws SQLException {
		boolean result = false;
		this.dbControl.getCon().setAutoCommit(false);
		PreparedStatement statement = this.dbControl.getCon().prepareStatement(INSERT_ATTR_QUERY);
		int count = 0;
		for (Map.Entry<String, String> e : attributes.entrySet()) {
			String attrType = e.getValue();
			String attrValue = e.getKey();
			//check if attribute exists in DB
			if(!this.existInDatabase(attrValue)) {
				statement.setInt(1, getTypeID(attrType));
				statement.setString(2, attrValue);
				count += statement.executeUpdate();
			}
			
			int attrID = this.getCurrenltyID(attrValue);
			this.mapAttributeToExpectations(attrID);

			this.dbControl.getCon().commit();
		}
		statement.close();
		this.dbControl.getCon().setAutoCommit(true);
		
		if(count > 0) {
			result = true;
		}
		return result;
	}

	@Override
	public boolean update(List<? extends Object> toUpdate) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean delete(String query) {
		// TODO Auto-generated method stub
		return false;
	}

}
